//
//  YWGraffitiController.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/7/28.
//

#import "YWGraffitiController.h"
#import "YWGraffitiView.h"
#import "YWColorChooseController.h"
#import "YWColorPopoverAnimator.h"

@interface YWGraffitiController ()
- (IBAction)dismissClick:(UIButton *)sender;
- (IBAction)conformBtnClick:(UIButton *)sender;


@property (weak, nonatomic) IBOutlet UIView *handleView;

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) YWGraffitiView *drawView;

@property(nonatomic,assign)BOOL isAlreadyAppear;

@property (weak, nonatomic) IBOutlet UIView *colorBgView;
- (IBAction)multiColorBtnClick:(UIButton *)sender;
- (IBAction)sliderChanged:(UISlider *)sender;

- (IBAction)previousBtnClick:(UIButton *)sender;
- (IBAction)removeAllBtnClick:(UIButton *)sender;

@property(nonatomic,strong)YWColorPopoverAnimator *animator;//弹框动画
@property (weak, nonatomic) IBOutlet UIView *colorSuperBgView;

@end

@implementation YWGraffitiController

#pragma mark - 懒加载
-(YWColorPopoverAnimator *)animator{
    if (_animator == nil) {
        _animator = [[YWColorPopoverAnimator alloc] init];
        MJWeakSelf
        [_animator animatorCallBackBlock:^(BOOL presented) {
            
            //这里可以知道现在是收起还是展开
        }];
    }
    return _animator;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    //给imageView原图
    self.imageView.image = self.image;
    
    //创建画view并添加到bgView
    YWGraffitiView *drawView = [[YWGraffitiView alloc] init];
    self.drawView = drawView;
    [self.bgView addSubview:drawView];
}

//初始化UI
-(void)setupUI{
    
    self.view.alpha = 0;
    self.handleView.transform = CGAffineTransformMakeTranslation(0, 300);
    
    NSArray *colorArray = @[[UIColor blackColor],[UIColor redColor],[UIColor yellowColor],[UIColor blueColor],[UIColor greenColor],[UIColor brownColor]];
    
    for (UIColor *color in colorArray) {
        UIButton *colorBtn = [[UIButton alloc] init];
        colorBtn.backgroundColor = color;
        [self.colorBgView addSubview:colorBtn];
        [colorBtn addTarget:self action:@selector(colorBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    CGFloat margin = 10;
    CGFloat width = (self.colorBgView.frame.size.width - (self.colorBgView.subviews.count + 1) * margin)*1.0 / self.colorBgView.subviews.count;
    CGFloat height = width;
    CGFloat y = (self.colorBgView.frame.size.height - height)/2;
    for (NSInteger i = 0; i < self.colorBgView.subviews.count; i++) {
        UIButton *colorBtn = self.colorBgView.subviews[i];
        
        CGFloat x = margin + (width + margin) * i;
        colorBtn.frame = CGRectMake(x, y, width, height);
        colorBtn.layer.cornerRadius = height/2;
    }
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:0.4 animations:^{
        
        self.view.alpha = 1;
        
        self.handleView.transform = CGAffineTransformIdentity;
    }];
    
    
    //显示第一张底图层
    if (self.isAlreadyAppear == NO){
        
        self.isAlreadyAppear = YES;
        
        self.imageView.hidden = YES; //不是对比的话一开始imageView就要隐藏
        
        MJWeakSelf
        
        CGFloat w = self.bgView.frame.size.width;
        CGFloat h = w * self.image.size.height*1.0/self.image.size.width;
        CGFloat y = 0;
        CGFloat x = 0;
        if (h > self.bgView.frame.size.height) {
            h = self.bgView.frame.size.height;
            y = 0;
            w = h * self.image.size.width*1.0/self.image.size.height;
            x = (self.bgView.frame.size.width - w)*1.0/2;
            
            [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.equalTo(weakSelf.drawView.superview).with.offset(x);
               make.right.equalTo(weakSelf.drawView.superview).with.offset(-x);
               make.bottom.equalTo(weakSelf.drawView.superview).with.offset(0);
               make.top.equalTo(weakSelf.drawView.superview).with.offset(0);
            }];
            
        }
        else{
            x = 0;
            w = self.bgView.frame.size.width;
            h = w * self.image.size.height*1.0/self.image.size.width;
            y = (self.bgView.frame.size.height - h)*0.5;
            
            
            [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.equalTo(weakSelf.drawView.superview).with.offset(0);
               make.right.equalTo(weakSelf.drawView.superview).with.offset(0);
                make.top.equalTo(weakSelf.drawView.superview).with.offset(y);
               make.bottom.equalTo(weakSelf.drawView.superview).with.offset(-y);
            }];
            
        }
        
        //设置第一张原始图片
        [self.drawView setFirstImage:self.image];
    }
    
    
    
}

-(void)dismiss{
    [UIView animateWithDuration:0.4 animations:^{
        
        self.handleView.transform = CGAffineTransformMakeTranslation(0, 300);
        self.view.alpha = 0;
    }completion:^(BOOL finished) {
        
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

- (IBAction)dismissClick:(UIButton *)sender {
    [self dismiss];
}

- (IBAction)conformBtnClick:(UIButton *)sender {
    
    if([self.delegate respondsToSelector:@selector(graffitiController:newImage:)]){
        [self.delegate graffitiController:self newImage:[self.drawView getCurrentLayer]];
    }
    
    [self dismiss];
    
}


-(void)colorBtnClick:(UIButton *)colorBtn{
    
    [self.drawView setLineColor:colorBtn.backgroundColor];
    
}

- (IBAction)removeAllBtnClick:(UIButton *)sender {
    [self.drawView removeAllHandle];
}

- (IBAction)previousBtnClick:(UIButton *)sender {
    [self.drawView previousHandle];
}

- (IBAction)sliderChanged:(UISlider *)sender {
    
    [self.drawView setLineWith:sender.value];
}


- (IBAction)multiColorBtnClick:(UIButton *)sender {
    // 1.创建弹出的控制器
    YWColorChooseController *vc = [[YWColorChooseController alloc] initWithNibName:@"YWColorChooseController" bundle:nil];
    
    // 2.设置控制器的modal样式
    vc.modalPresentationStyle = UIModalPresentationCustom;
    
    // 3.设置转场的代理
    vc.transitioningDelegate = (id<UIViewControllerTransitioningDelegate>)self.animator;
    
    
    CGRect senderRects = [self.colorSuperBgView convertRect:sender.frame toView:[UIApplication sharedApplication].keyWindow];
    
    NSLog(@"%@",NSStringFromCGRect(senderRects));
    
    CGFloat w = 225;
    CGFloat h = 250;
    CGFloat x = CGRectGetMaxX(senderRects) - w;
    CGFloat y = CGRectGetMinY(senderRects) - h;
    
    // 4.设置弹出VC的大小
    self.animator.presentedFrame = CGRectMake(x, y, w, h);
    
    // 5.弹出控制器
    [self presentViewController:vc animated:YES completion:nil];
    
    vc.didSelectColorBlock = ^(UIColor * _Nonnull color) {
        
        [self.drawView setLineColor:color];
        
        
    };
    
}


@end
